package com.amat0urheu.service.cloudService.impl;

import com.amat0urheu.dao.CloudMapper;
import com.amat0urheu.pojo.CloudPojo.OpenStack;
import com.amat0urheu.pojo.CloudPojo.OpenStackNetwork;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.service.cloudService.CloudService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CloudServiceImpl implements CloudService {
    @Autowired
    CloudMapper cloudMapper;
    @Override
    public ReturnRes getAllOpenstack(Integer pageSize,Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<OpenStack> openstackList = cloudMapper.getAllOpenstack();

        if (openstackList != null) {
            return new ReturnRes(true, new PageInfo<>(openstackList), "查询成功");
        }
        return new ReturnRes(false, "查询失败");
    }

    @Override
    public ReturnRes getAllOpenstackIPS(Integer pageSize, Integer pageNum) {

        PageHelper.startPage(pageNum, pageSize);
        List<String> openstacIPS = cloudMapper.getAllOpenstackIP();
        List<OpenStackNetwork> openStackNetworkList=new ArrayList<>();
        for (int i=0;i<openstacIPS.size();i++){
            OpenStackNetwork  openStackNetwork=new OpenStackNetwork();
            openStackNetwork.setOpenstackIP(openstacIPS.get(i));
            openStackNetwork.setNetworkNumber(cloudMapper.getNetworksNumByOpenstackIP(openstacIPS.get(i)));
            openStackNetworkList.add(openStackNetwork);
        }
        if (openstacIPS != null) {
            return new ReturnRes(true, new PageInfo<>(openStackNetworkList), "查询成功");
        }
        return new ReturnRes(false, "查询失败");
    }

    @Override
    public List<OpenStack> getOpenstacks() {

        return  cloudMapper.getAllOpenstack();
    }

    @Override
    public List<String> getAllOpenstackIP() {
        List <String> OpentsackIPs=cloudMapper.getAllOpenstackIP();

        return  OpentsackIPs;
    }

    @Override
    public ReturnRes updateOpenstack(OpenStack openStack) {

      int count= cloudMapper.updateOpenstack(openStack);
        if(count==1){
            return new ReturnRes(true, "修改成功");
        }

        return new ReturnRes(false, "修改失败");
    }

    @Override
    public ReturnRes saveOpenstack(OpenStack openStack) {
        int count=  cloudMapper.saveOpenstack(openStack);
        if(count==1){
            return new ReturnRes(true, "修改成功");
        }
        return new ReturnRes(false, "修改失败");
    }

    @Override
    public ReturnRes removeOpenstackById(Integer id) {
        int count=  cloudMapper.removeOpenstackById(id);
        if(count==1){
            return new ReturnRes(true, "删除成功");
        }

        return new ReturnRes(false, "删除失败");
    }

}
